package edu.uwosh.cs342.placestobe;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.StringTokenizer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GoogleJSON {

	public static String[] test(double lat, double lng, String type) {
		try {  	
			URL url = new URL("https://maps.googleapis.com/maps/api/place/search/json?location="+lat+","+lng+"&radius=7500&types="+type+"&sensor=false&key=AIzaSyC2p2B6GKw3BzbYV1c3r6erQDkce3dE9mw");
		    InputStream in = url.openStream();
		    InputStreamReader reader = new InputStreamReader(in);    
			 Gson gson = new GsonBuilder().create();
			 Data data = gson.fromJson(reader, Data.class); 			 
 			 StringTokenizer st = new StringTokenizer(data.toString(), "|");
 			 String places[] = new String[st.countTokens()];
 			 int n = 0;
 			 while(st.hasMoreElements()){
 				 if(st.equals(null) || st.toString() == null ||st.toString() =="null"){
 					 
 				 }else{
 					 places[n] = (String) st.nextElement();
 					 n++;
 				 }
 			 }
 			 reader.close();
 			 return places;
		} catch (Exception e) 
		{
			String places[] = new String[1];
			places[0] = e.toString();
			return places; 
		}
		
	}
		 	
 	class Data {
 		//private List<Data> html_attributions;
 	    private List<Data> results;
 	    private Geo geometry;
 	    private String name;

 	   // public Geo getLoc() { return geometry; }
 	    public String getName() { return name; }
 	    //public void setGroups(List<Data> results) { this.results = results; }
 	    
 	    @Override
 	    public String toString() {
 	    	if(results == null){
 	    		 return  name+":"+ geometry;
 	    	}
 	    	else 
 	    		return ""+ results; //+ String.format("results: name:%s geometry:%s", name,geometry);
 	    }
 	}
 	class Geo {
 		private loc location;
 		
 		 @Override
 		  public String toString() {
 		    return location.lat + " " + location.lng +"|";
 		  }
 	}
 	class loc {
 		
 		private double lat;
 		private double lng;
 		 @Override
 		  public String toString() {
 		    return lat + "  " + lng;
 		  }
 		
 	}

 	
 }






